import {request} from "../../utils/util";
import {baseUrl} from "../../config";

Page({
  data: {
    form: {
      venue_id: '',
      contents: '',
      star: 4,
      img: []
    }
  },
  onInputChange(e) {
    const { value } = e.detail;
    const { name } = e.currentTarget.dataset;
    const { form } = this.data;
    this.setData({
      form: { ...form, [name]: value }
    });
  },
  save() {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/venue/comment',
      method: 'POST',
      data: {
        ...this.data.form,
        star: this.data.form.star + 1
      },
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          wx.showToast({
            title: '提交成功！',
            icon: 'success',
            duration: 2000
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1000)
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  del (e) {
    let { index } = e.currentTarget.dataset
    this.data.form.img.splice(index, 1)
    this.setData({
      form: this.data.form
    })
  },
  chooseImg () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        let files = res.tempFilePaths
        wx.showLoading({
          title: '加载中..',
          mask: true
        })
        wx.uploadFile({
          url: `${baseUrl}/api/file`,
          filePath: files[0],
          name: 'img',
          formData: {
            type: 'comment'
          },
          success: (result) => {
            wx.hideLoading()
            if (result.statusCode === 200) {
              let data = JSON.parse(result.data)
              if (data.code === 200) {
                this.data.form.img.push(data.data.url)
                this.setData({
                  form: this.data.form
                })
              } else {
                wx.showToast({
                  title: data.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            } else {
              wx.showToast({
                title: result.errMsg,
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail(err) {
            console.log(err)
          }
        })
      }
    })
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.data.form.venue_id = this.options.venue_id
    this.data.form.order_id = this.options.order_id
  },
  doStar (e) {
    this.setData({
      form: {
        ...this.data.form,
        star: e.currentTarget.dataset.index
      }
    })
  },
})
